//
//  UserAccount.swift
//  sinaWeibo
//
//  Created by 张国梁 on 16/4/9.
//  Copyright © 2016年 CoderGL. All rights reserved.
//

import UIKit

class UserAccount: NSObject, NSCoding {
    var access_token : String?
    var expires_in : NSNumber? {
        didSet {
            expiresDate = NSDate(timeIntervalSinceNow: expires_in!.doubleValue)
        }
    }
    var uid : String?
//    var remind_in : NSNumber?
    var expiresDate : NSDate? // 过期时间
    var avatar_large : String? // 头像大图
    var screen_name : String? // 用户昵称
    
    override init() {
        super.init()
    }
    
    // KVC字典转属性
    init(dict : [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        print(key)
    }
    
    // 获取用户信息
    func loadUserInfo(finished : (account : UserAccount?, error : NSError?)->()) -> Void {
        let path = "/2/users/show.json"
        let params = ["access_token" : access_token!, "uid" : uid!]
        
        NetworkTools.shareNetworkTools().GET(path, parameters: params, progress: nil, success: { (_, JSON) in
            if let dict = JSON as? [String : AnyObject] {
                self.avatar_large = dict["avatar_large"] as? String
                self.screen_name = dict["screen_name"] as? String
                finished(account: self, error: nil)
            } else {
                finished(account: nil, error: nil)
            }
        }) { (_, error) in
            
            finished(account: nil, error: error)
        }
        
        
    }
    
    
    // 是否已经授权
    class func isOAuth() -> Bool {
        return self.getAccountInfo() != nil
    }
    
    // 存储读取用户帐号信息
    func saveAccountInfo() {
        NSKeyedArchiver.archiveRootObject(self, toFile: "account.plist".cacheDir())
        print("account.plist".cacheDir())
    }
    
    
    static var account : UserAccount?
    class func getAccountInfo() -> UserAccount? {
        
        if account != nil {
            return account
        }
        
        account = NSKeyedUnarchiver.unarchiveObjectWithFile("account.plist".cacheDir()) as? UserAccount
        
        // 如果过期时间比当前时间小 说明过期了
        if account?.expiresDate?.compare(NSDate())==NSComparisonResult.OrderedAscending  {
            return nil
        }
        
        return account
    }
    
    
    // 归档反归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
    }
    
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeObjectForKey("expires_in") as? NSNumber
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    }
    
    
    override var description: String {
        let arrStr = ["access_token", "expires_in", "uid", "expiresDate"]
        let dict = self.dictionaryWithValuesForKeys(arrStr)
        return "\(dict)"
    }
}
